package com.douwong.student.utils;

import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.douwong.student.application.BaseApplication;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;
import java.io.StringWriter;


public class JsonUtils<T> {
	private static final String TAG = "JsonUtils";

	private static ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * 解析判断json格式
	 * @param json
	 * @param clazz
	 * @param <T>
     * @return
     */
	public static<T> T parseString(String json,Class<T> clazz){
		if (json!=null && !json.startsWith("{")&&!json.endsWith("}")){
			Toast.makeText(BaseApplication.mContext,"json格式错误",Toast.LENGTH_LONG).show();
			return null;
		}
		return JSON.parseObject(json,clazz);

	}

	/**
	 * 解析判断json格式
	 * @param obj
	 * @param clazz
	 * @param <T>
     * @return
     */
	public static<T> T parseObject(Object obj,Class<T> clazz){
			String json = serialize(obj);


		if (json!=null && !json.startsWith("{")&&!json.endsWith("}")){
			Toast.makeText(BaseApplication.mContext,"json格式错误",Toast.LENGTH_LONG).show();
			Log.i(TAG,"json格式错误 :"+json);
			return null;
		}
		return JSON.parseObject(json,clazz);

	}



	public static String serialize(Object object) {
		StringWriter write = new StringWriter();
		try {

			objectMapper.writeValue(write, object);
			write.close();
		} catch (JsonGenerationException e) {
			//logger.error("JsonGenerationException when serialize object to json", e);
		} catch (JsonMappingException e) {
			//logger.error("JsonMappingException when serialize object to json", e);
		} catch (IOException e) {
			//logger.error("IOException when serialize object to json", e);
		}
		return write.toString();
	}

	public static <T> T deserializw(Object obj,Class<T> clazz){



		Object object = null;
		try {
			String json = serialize(obj);
			object = objectMapper.readValue(json, TypeFactory.rawClass(clazz));
		} catch (JsonParseException e) {
			//logger.error("JsonParseException when serialize object to json", e);
		} catch (JsonMappingException e) {
			//logger.error("JsonMappingException when serialize object to json", e);
		} catch (IOException e) {
			//logger.error("IOException when serialize object to json", e);
		}

		return (T)object;
	}

	public static <T> T deserialize(String json, Class<T> clazz) {
		Object object = null;
		try {
			objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			object = objectMapper.readValue(json, TypeFactory.rawClass(clazz));
		} catch (JsonParseException e) {
			//logger.error("JsonParseException when serialize object to json", e);
		} catch (JsonMappingException e) {
			LogUtils.i("JsonMappingException :", e.toString());
		} catch (IOException e) {
			//logger.error("IOException when serialize object to json", e);
		}
		return (T) object;
	}
	public static <T> T deserialize(String json, com.fasterxml.jackson.core.type.TypeReference typeRef) {
		try {
			return (T) objectMapper.readValue(json, typeRef);
		} catch (JsonParseException e) {
			//logger.error("JsonParseException when deserialize json", e);
		} catch (JsonMappingException e) {
			//logger.error("JsonMappingException when deserialize json", e);
		} catch (IOException e) {
			//logger.error("IOException when deserialize json", e);
		}
		return null;
	}


	
}
